package com.fangtao.gps.server;

import java.util.concurrent.TimeUnit;

import com.fangtao.gps.hanlder.GpsHandler;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.timeout.IdleStateHandler;

public class GpsServer {

	public static void main(String[] args) {
		
			ServerBootstrap serverBootstrap = new ServerBootstrap();

			NioEventLoopGroup boos = new NioEventLoopGroup();
			NioEventLoopGroup worker = new NioEventLoopGroup();

			serverBootstrap.group(boos, worker).channel(NioServerSocketChannel.class)
					.childHandler(new ChannelInitializer<NioSocketChannel>() {

						protected void initChannel(NioSocketChannel ch) {
							ChannelPipeline pipeline = ch.pipeline();
							pipeline.addLast(new StringDecoder()); // 编码
							pipeline.addLast("ping", new IdleStateHandler(600, 0, 0,TimeUnit.SECONDS));
							pipeline.addLast("handler", new GpsHandler()); // 自定义处理器
						}

					}).bind(8666);

	}
	
	
	
	
	
	
	

	void test01() {
//		final ByteBuf buf = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", Charset.forName("UTF-8")));

		ServerBootstrap serverBootstrap = new ServerBootstrap();

		NioEventLoopGroup boos = new NioEventLoopGroup();
		NioEventLoopGroup worker = new NioEventLoopGroup();

		serverBootstrap.group(boos, worker).channel(NioServerSocketChannel.class)
				.childHandler(new ChannelInitializer<NioSocketChannel>() {

					protected void initChannel(NioSocketChannel ch) {
						ChannelPipeline pipeline = ch.pipeline();

						pipeline.addLast(new StringDecoder());

//						pipeline.addLast(new ChannelInboundHandlerAdapter() {
//							@Override
//							public void channelActive(ChannelHandlerContext ctx) throws Exception {
//								while (true) {
//									ctx.writeAndFlush(buf.duplicate()).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
//									Thread.sleep(1000);
//								}
//							}
//						});

//						pipeline.addLast(new SimpleChannelInboundHandler<String>() {
//							@Override
//							protected void channelRead0(ChannelHandlerContext ctx, String msg) {
//								
//								System.out.println("------接收客户端信息------" + msg);
//							}
//						});

						pipeline.addLast("handler", new GpsHandler());

					}

				}).bind(8000);

	}

}
